
console.log("//-------------testMyPromise-----------------")
alert("begin test");
// contructor
var MyPromise = function(){
    this.callbacks = [];
}

MyPromise.prototype = {
    construct: MyPromise,
    resolve: function(result){
        this.complete("resolve", result);
    },
    reject: function(result){
        this.complete("reject", result);
    },
    complete: function(type, result){
        while(this.callbacks[0]){
            this.callbacks.shift()[type](result);
        }
    },
    then: function(succHandler, failedHandler){
        this.callbacks.push({
            resolve: succHandler,
            reject: failedHandler
        })
        return this;
    }
}

// var mypromise = new MyPromise();

// setTimeout(function(){       
//     mypromise.resolve("data1");
//     }, 1000);

// callback1 = function(re){
//     re = re+"data1";
//     document.write("<p>"+re+"</p>");
// }
// mypromise.then(callback1);

var g_resolve;
var g_reject;

function callback2(resolve, reject){
    g_resolve = resolve;
    g_reject = reject;
}
// 创建Promise对象
var mypromise2 = new Promise(callback2);
// 或者
var mypromise3 = new Promise(function(resolve, reject){
    g_resolve = resolve;
    g_reject = reject;
})


// 或者直接在函数里执行代码
var mypromise4 = new Promise(function(resolve, reject){
    var icnt;
    for(icnt=0; icnt< 10000; icnt++){
        icnt = icnt*2;
    }
    
    //if(icnt == 10000)
    // resolve("process success");
    g_resolve = resolve;
    //else
    //    reject("failed");
    console.log("end new Promise")
})
console.log("before function success");
function success(ret){
    console.log("success: "+ret);
    return "success return";
}

console.log("before function failed");
function failed(ret){
    console.log("failed: "+ret);
    return "failed return";    
}
console.log("before function success1");
function success1(ret){
    console.log("success1: "+ret);
    return "success1 return";
}
console.log("before function failed1");
function failed1(ret){
    console.log("failed1: "+ret);
    return "failed1 return";
}
console.log("before function success2");
function success2(ret){
    console.log("success2: "+ret);
    return "success2 return";
}
console.log("before function failed2");
function failed2(ret){
    console.log("failed2: "+ret);
    return "failed2 return";
}
console.log("before function success3");
function success3(ret){
    console.log("success3: "+ret);
    return "success3 return";
}
console.log("before function failed3");
function failed3(ret){
    console.log("failed3: "+ret);
    return "failed3 return";
}
console.log("before mypromise4.then");
// 上面的函数执行完成后，调用then设置的2个方法
let mpypromise5 = mypromise4.then(success, failed);

console.log("before mpypromise5.then1");
// success或falled方法执行完成后，再执行success1或falled1
// success或falled的返回值作为success1或falled1的参数
mpypromise5.then(success1,failed1);
console.log("before mpypromise5.then2");
// 再追加一个tnen，then方法相当于append添加处理
mpypromise5.then(success2,failed2);
mpypromise5.then(success2,failed2);

setTimeout(function(){
        console.log("timeout process 1000");
        g_resolve("g_resolve");
    }, 1000);

setTimeout(function(){
        console.log("timeout process 3000");
        // 事件已经为完成状态，调用则直接执行success3或failed3方法
        mpypromise5.then(success3,failed3);
    }, 3000);













